package com.wx.studentManage.service.Impl;

import com.wx.studentManage.mapper.UserMapper;
import com.wx.studentManage.model.Team;
import com.wx.studentManage.model.User;
import com.wx.studentManage.service.UserService;
import com.wx.studentManage.util.MyDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String register(User user) {
        // 验证账号
        User u = userMapper.findUserByUserCode(user.getUserCode());
        if (u == null){
            user.setCreateTime(MyDate.getDate());
            user.setUpdateTime(MyDate.getDate());
           return userMapper.saveUser(user) == 1 ? "success" : "false";
        }else{
            return "repeat";
        }
    }

    @Override
    public String login(User user, HttpServletRequest request) {
            User u = userMapper.findByUserCodeAndPassword(user);
            if (u == null){
                return "用户名或者密码错误";
            } else {
                return "1";
            }
    }

    @Override
    public String loginRedis(User user, HttpServletRequest request) {
        String code = user.getCode().toLowerCase();
        String checkCode = user.getCheckCode().toLowerCase();

        if(checkCode != null && checkCode.equals(code)) {// 判断验证码是否正确
        User u = userMapper.findByUserCodeAndPassword(user);
        if (u == null){
            return "用户名或者密码错误";
        } else {
            return "1";
        }
        }
        return "验证码错误";
    }

    @Override
    public int perfect(User user) {
        user.setUpdateTime(MyDate.getDate());
        return userMapper.updateUser(user);
    }

    @Override
    public User findUserByUserCode(String userCode) {
        return userMapper.findUserByUserCode(userCode);
    }

    @Override
    public List<User> getUserList(Map<String, Object> data) {
        int page = Integer.parseInt((String) data.get("pageNum"));
        int size = Integer.parseInt((String)data.get("pageSize"));
        data.put("pageNum",(page-1)*size);
        data.put("pageSize",size);
        return userMapper.getUserList(data);
    }

    @Override
    public int getSelectCount(Map<String, Object> data) {
        return userMapper.getSelectCount(data);
    }

    @Override
    public int updateState(User user) {
        user.setUpdateTime(MyDate.getDate());
        return userMapper.updateState(user);
    }

    @Override
    public List<User> getUserByTeamCode(String teamCode) {
        return userMapper.getUserByTeamCode(teamCode);
    }

    @Override
    public List<User> getUserItemByNoTeamCode() {
        return userMapper.getUserItemByNoTeamCode();
    }

    @Override
    public String updateUserTeam(String teamCode, List<Integer> ids) {
        Map params = new HashMap();
        params.put("teamCode",teamCode);
        params.put("ids",ids);
        params.put("updateTime",MyDate.getDate());
        int result = userMapper.updateUserTeam(params);
        if (result>0){
            return "success";
        }else {
            return "false";
        }
    }

    @Override
    public int getCountByTeam(String teamCode) {
        return userMapper.getCountByTeam(teamCode);
    }

    @Override
    public String updateUserRole(String teamCode, String userCode) {
        User user = new User();
        user.setTeamCode(teamCode);
        user.setUserCode(userCode);
        user.setUpdateTime(MyDate.getDate());
        int result = userMapper.updateUserToStu(user);
        int data = userMapper.updateUserToLea(user);

        if (result > 0 && data > 0){
            return "success";
        } else {
            return "false";
        }
    }

    @Override
    public String updateLeaderToStu(Integer id) {
        int result = userMapper.updateLeaderToStu(id);
        if (result > 0){
            return "success";
        } else {
            return "false";
        }
    }

}
